<?php
/**
* PDF.class.php
*
* Class for code generation
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/

require_once('framework/config.php');
require_once('fpdf.php');



class PDF extends FPDF
{
    function PDF ( $_options = array() )
    {
        $this->setDefaultValues();
        $this->parseOptions( $_options );
        $this->FPDF($this->options['orientation'], 
                    $this->options['pageUnit'], 
                    array( $this->options['pageWidth'], $this->options['pageHight'] )
                    );
    }

    function parseOptions ( $options ) 
    {
        foreach ( $this->defaultOptions as $key => $value )
        {
            if ( isset($options[$key]) )
            {
                $this->options[$key] = $value;
            }
            else
            {
                $this->options[$key] = $this->defaultOptions[$key];
            }
        }

    }

    function setDefaultValues()
    {
        
        $this->defaultOptions = array (    
        'orientation' => 'P',
        'pageWidth' => 210,
        'pageHight' => 297,
        'pageUnit'  => 'mm',
        'hight' => 57,
        'width' => 100,
        'left_margin' => 0,
        'right_margin' => 0,
        'top_margin' => 0,
        'fontSize' => 6,
        'code_margin' => 30        
        );
    }

    function setContent( $_content = array() )
    {
        $this->content = $_content;
    }

    function createPage()
    {
        
        $this->AddPage();

        $this->SetFont('Arial','B',$this->options['fontSize']);
        $this->SetMargins($this->options['left_margin'],$this->options['top_margin'],$this->options['right_margin']);

        $maxCols = floor( ( $this->options['pageWidth'] - ( $this->options['right_margin'] + $this->options['left_margin'] )  ) / $this->options['width'] );
        $maxRows= floor( ( $this->options['pageHight'] - 2 * $this->options['top_margin'] ) / $this->options['hight'] );
        
        for($i = 0 ; $i < count($this->content) ; $i++)
        {
            
            $currentRow = floor($i/$maxCols) ; 
            $currentPage = floor ( $currentRow / $maxRows );
            $currentX = $this->options['left_margin'] + ($this->options['width'] * ($i%$maxCols) ) ;
            $currentY = ( ( $currentRow -  ( $currentPage*$maxRows ) ) * $this->options['hight'] + $this->options['top_margin'] ) ;
            $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');

            $url = "http://chart.apis.google.com/chart?chld=|1&chs=150x150&cht=qr&chl=".$global['service_url']."?go=".$this->content[$i]['guide_id']."-".$this->content[$i]['slide_id']."&choe=UTF-8";
         
            $ch = curl_init();
         
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER,0);
            curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
            curl_setopt($ch, CURLOPT_PROXY,  isset($global['proxy']) ? $global['proxy'] : null ); 

            $data = curl_exec($ch);
            curl_close($ch);

            $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');

            $path =$upload['upload_dir'];

            if ($path == '')
            {
                trigger_error("AdminSendCodeFormModule::getContent() upload dir not set", E_USER_ERROR);
            }

            $fileName = $path."/".md5(uniqid(rand(), true)).".png";

            $fp = fopen($fileName, 'w');
            fwrite($fp, $data);
            fclose($fp);


            $this->Image($fileName, 
                         $currentX  + ($this->options['code_margin']/2),
                         $currentY  + ($this->options['code_margin']/2),
                         $this->options['hight'] - $this->options['code_margin']/2 ,
                         $this->options['hight'] - $this->options['code_margin']/2,
                         'PNG');

            if ( !unlink( $fileName ) )
            {
                trigger_error("AdminSendCodeFormModule::getContent() Could not remove file".$fileName, E_USER_ERROR);
            }

            $this->SetXY($currentX+$this->options['hight'],$currentY + ($this->options['code_margin']/2));
            $this->Write($this->options['fontSize']/2,$this->content[$i]['title']);

            $this->SetXY($currentX+$this->options['hight'] ,$currentY+$this->options['fontSize']/2 + ($this->options['code_margin']/2));
            $this->Write($this->options['fontSize']/2, $this->content[$i]['subTitle']);

            $this->SetXY($currentX+$this->options['hight'] ,$currentY+$this->options['fontSize'] + ($this->options['code_margin']/2));
            $this->Write($this->options['fontSize']/2, $this->content[$i]['desc']);

            if ( (($i+1)%$maxCols) == 0 )
            {
                $this->Ln();
            }
            
            if ( ($i+1)%($maxCols*$maxRows) == 0 )
            {   
                $this->AddPage();
            }

        }

    }
    

}
?>

